package org.Multibot;

import javax.swing.filechooser.FileSystemView;
import java.awt.*;
import java.io.BufferedWriter;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Properties;
import java.util.logging.FileHandler;
import java.util.logging.LogManager;

public class Configuration {

	public static final String NAME = "MultiBot";
	public static final String SITE = "http://www.multibot.org";
	public static String LANGUAGE = "";
	
    private static String os;
	public enum OperatingSystem {
		MAC, WINDOWS, LINUX, UNKNOWN
	}
	private static final OperatingSystem CURRENT_OS;
	static {
		final String os = System.getProperty("os.name");
		if (os.contains("Mac")) {
			CURRENT_OS = OperatingSystem.MAC;
		} else if (os.contains("Windows")) {
			CURRENT_OS = OperatingSystem.WINDOWS;
		} else if (os.contains("Linux")) {
			CURRENT_OS = OperatingSystem.LINUX;
		} else {
			CURRENT_OS = OperatingSystem.UNKNOWN;
		}
	}
	public static final String root = "./Resources/";
	public static final String root_images = "./Resources/images/";
	public static final String root_images_hiscores = "./Resources/images/hiscores/";
	
	public static final String str_hiscore = root_images_hiscores +"str.gif";
	public static final String def_hiscore = root_images_hiscores +"def.gif";
	public static final String att_hiscore = root_images_hiscores +"att.gif";
	public static final String range_hiscore = root_images_hiscores +"ran.gif";
	public static final String magic_hiscore = root_images_hiscores +"magic.gif";
	public static final String pray_hiscore = root_images_hiscores +"pray.gif";
	public static final String hp_hiscore = root_images_hiscores +"cons.gif";
	public static final String summon_hiscore = root_images_hiscores +"sum.gif";

	public static void openURL(String url) {
        os = System.getProperty("os.name").toLowerCase();
        Runtime rt = Runtime.getRuntime();
        try {
			if (os.indexOf( "win" ) >= 0) {
                rt.exec( "rundll32 url.dll,FileProtocolHandler " + url);
            } else if (isMac()) {
                rt.exec( "open " + url);
			} else if (os.indexOf( "nix") >=0 || os.indexOf( "nux") >=0) {
                String[] browsers = {"google", "chrome", "epiphany", "firefox", "mozilla", "konqueror",
                                     "netscape","opera","links", "lynx" };
                StringBuilder cmd = new StringBuilder();
                for (int i=0; i<browsers.length; i++)
                    cmd.append(i == 0 ? "" : " || ").append(browsers[i]).append(" \"").append(url).append( "\" ");
                    rt.exec(new String[] { "sh", "-c", cmd.toString() });
				} else {
                    return;
                }
            } catch (Exception e){
                return;
            }
        return;
    }
	public static boolean isMac() {
        if (os.indexOf( "mac" ) >= 0) {
            return true;
		} else {
            return false;
        }
    }
	
	public static OperatingSystem getCurrentOperatingSystem() {
		return Configuration.CURRENT_OS;
	}
	
	public static String getHttpUserAgent() {
		String plat = "Windows", os = "Windows NT 5.2";
		if (Configuration.getCurrentOperatingSystem() == Configuration.OperatingSystem.MAC) {
			plat = "Macintosh";
			os = "Intel Mac OS X 10_6_4";
		} else if (Configuration.getCurrentOperatingSystem() != Configuration.OperatingSystem.WINDOWS) {
			plat = "X11";
			os = "Linux i686";
		}
		StringBuilder buf = new StringBuilder(125);
		buf.append("Mozilla/5.0 (").append(plat).append("; U; ").append(os);
		buf.append("; en-US) AppleWebKit/534.7 (KHTML, like Gecko) Chrome/7.0.517.44 Safari/534.7");
		return buf.toString();
	}

	public static URLConnection getURLConnection(final URL url, final String referer) throws IOException {
		final URLConnection con = url.openConnection();
		con.addRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
		con.addRequestProperty("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7");
		con.addRequestProperty("Accept-Encoding", "gzip,deflate");
		con.addRequestProperty("Accept-Language", "en-us,en;q=0.5");
		con.addRequestProperty("Connection", "keep-alive");
		con.addRequestProperty("Host", url.getHost());
		con.addRequestProperty("Keep-Alive", "300");
		if (referer != null) {
			con.addRequestProperty("Referer", referer);
		}
		con.addRequestProperty("User-Agent", getHttpUserAgent());
		return con;
	}	
}
